
import 'package:find_friend/common/event_bus.dart';
import 'package:find_friend/const/const.dart';
import 'package:find_friend/pages/im/message.dart';
import 'package:find_friend/pages/mine/mine.dart';
import 'package:find_friend/pages/school/school_page.dart';
import 'package:find_friend/pages/starRoom/starScreen.dart';
import 'package:find_friend/pages/xspace/xpage.dart';
import 'package:find_friend/root/tab_animation.dart';
import 'package:find_friend/root/tab_selector.dart';
import 'package:flutter/material.dart';

class RootPage extends StatefulWidget {
  @override
  _RootPageState createState() => _RootPageState();
}

class _RootPageState extends State<RootPage> {

  final PageController _controller = PageController(initialPage: 2);
  
  int index = 2; // tabbar选择的是第几个
  bool hiddentabbar = false;
  XPage xpage = XPage();
  @override
  void initState() {
    super.initState();

    // 退出登录
    EventMineBus.instance.addListener(EventMineKeys.ExitLogin, (arg) {
      _controller.jumpToPage(2);
      setState(() {
        index = 2;
      });
    });


    // 隐藏tabbar
    EventMineBus.instance.addListener(EventMineKeys.HiddenTabbar, (arg) {
      setState(() {
        hiddentabbar = arg ?? false;
      });
    });

  }

  

  @override
  void dispose() {
    _controller.dispose();
    EventMineBus.instance.removeListener(EventMineKeys.ExitLogin);
    super.dispose();
  }

  _jumpToIndex(int sender) {
    // SharedUserInfo user = await SharedUserInfo.instance();
    // if(user.schoolname == null) {
      if(index == 1 && sender == 1) {
        xpage.refresh();
      }
      if(sender == index) return;
      _controller.jumpToPage(sender);
      setState(() {
        
        index = sender;
      });
    // } else {
    //   setState(() {
    //     index = 2;
    //   });
    //   AlertTool.showConfirmDialog(context, title: "请先认证学生证");
    // }
    
  }
  
  Widget _selectIconWidget(String iconname) {
    return Padding(
            padding: const EdgeInsets.only(bottom: 5.0, top: 5),
            child: Image.asset("assets/$iconname",width: 51, height: 51,),
          );
  }

  @override
  Widget build(BuildContext context) {
    final size = MediaQuery.of(context).size;
    final padding = MediaQuery.of(context).padding;
    return Scaffold(
      extendBody: true,
      body: PageView(
        controller: _controller,
        physics: NeverScrollableScrollPhysics(), // 禁止滑动
        children: [
          SchoolPage(),
          xpage,
          StarScreen(),
          MessagePage(),
          MineScreen()
        ],
      ),
      bottomNavigationBar: Offstage(
        offstage: hiddentabbar,
        child: TabbarSelector(
          index: index,
          // backgroundColor: Colors.white,
          // circleOffset: 0.25,
          // circleRadiuOffset: 10,
          children: [
            Image.asset("assets/xuexiao.png",width: 51, height: 51, fit: BoxFit.scaleDown),
            Image.asset("assets/chigua.png",width: 51, height: 51, fit: BoxFit.scaleDown),
            Padding(
              padding: const EdgeInsets.only(bottom: 5.0, top: 0),
              child: Image.asset("assets/qiu.png",width: 51, height: 51,),
            ),
            _TabMsgWidget(),
            Image.asset("assets/wode.png",width: 51, height: 51, fit: BoxFit.scaleDown,),
          ],
          selectedChildren: [
            _selectIconWidget("xuexiaoac.png"),
            _selectIconWidget("chiguaactive.png"),
            // _selectIconWidget("qiu.png"),
            Padding(
              padding: const EdgeInsets.only(bottom: 5.0, top: 0),
              child: Image.asset("assets/qiu.png",width: 51, height: 51,),
            ),
            _TabMsgWidget(isseleted: true,),
            _selectIconWidget("wodeActive.png"),
          ],
          onPress: _jumpToIndex,
        ),
      )
    );
  }
}

class _TabMsgWidget extends StatefulWidget {

  final bool isseleted;

  const _TabMsgWidget({Key key, this.isseleted}) : super(key: key);

  @override
  __TabMsgWidgetState createState() => __TabMsgWidgetState();
}

class __TabMsgWidgetState extends State<_TabMsgWidget> {

  int msgCount = 0;
  
  @override
  void initState() {
    super.initState();

    EventMineBus.instance.addListener(EventMineKeys.IMMessageCount, (arg) {
      setState(() {
        msgCount = arg < 0 ? 0 : arg;
      });
    });
  }

  @override
  Widget build(BuildContext context) {
    if(widget.isseleted == true) {
      return Stack(
              alignment: Alignment.center,
              children: [
                Padding(
                  padding: const EdgeInsets.only(bottom: 8.0, top: 5),
                  child: Image.asset("assets/liaotian.png",width: 51, height: 51,),
                ),
                Positioned(
                  top: 5,
                  right: 15,
                  child: Offstage(
                    offstage: msgCount == 0,
                    child: Container(
                      padding: const EdgeInsets.symmetric(horizontal: 4),
                      decoration: BoxDecoration(
                        color: APP_verify_error_color,
                        borderRadius: BorderRadius.circular(8)
                      ),
                      child: Text(msgCount > 99 ? "99+" : msgCount.toString(),style: instanceTextStyle(12, Colors.white),)
                    ),
                  ),
                )
              ],
            );
    } else {
      return Stack(
              alignment: Alignment.center,
              children: [
                Image.asset("assets/xiaoxi233.png",width: 51, height: 51, fit: BoxFit.scaleDown),
                Positioned(
                  top: 5,
                  right: 15,
                  child: Offstage(
                    offstage: msgCount == 0,
                    child: Container(
                      padding: const EdgeInsets.symmetric(horizontal: 4),
                      decoration: BoxDecoration(
                        color: APP_verify_error_color,
                        borderRadius: BorderRadius.circular(8)
                      ),
                      child: Text(msgCount > 99 ? "99+" : msgCount.toString(),style: instanceTextStyle(12, Colors.white),)
                    ),
                  ),
                )
              ],
            );
    }
    
  }
}


